package org.example.jiaxiao.controller;

import org.example.jiaxiao.entity.Account;
import org.example.jiaxiao.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class RegisterController {
    private static final Logger logger = LoggerFactory.getLogger(RegisterController.class);

    @Autowired
    private RegisterService registerService;

    @PostMapping("/register")
    public ResponseEntity<?> register(@RequestBody Map<String, String> registerRequest) {
        logger.info("Register request received: {}", registerRequest);

        if (registerRequest == null) {
            logger.error("Register request is null");
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", "请求数据不能为空");
            return ResponseEntity.ok(response);
        }

        String username = registerRequest.get("username");
        String password = registerRequest.get("password");
        String realName = registerRequest.get("realName");
        String phone = registerRequest.get("phone");
        String userType = registerRequest.get("userType");

        if (username == null || password == null || realName == null || phone == null || userType == null) {
            logger.error("Missing required fields");
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", "必填字段不能为空");
            return ResponseEntity.ok(response);
        }

        Account account = new Account();
        account.setUserName(username);
        account.setPassword(password); // 注意：实际应用中密码需要加密
        account.setTureName(realName);
        account.setPhone(phone);
        account.setUsertype(userType);

        try {
            boolean success = registerService.register(account);
            if (success) {
                logger.info("Registration successful for user: {}", username);
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("message", "注册成功");
                return ResponseEntity.ok(response);
            } else {
                logger.warn("Registration failed - Username already exists: {}", username);
                Map<String, Object> response = new HashMap<>();
                response.put("code", 409);
                response.put("message", "用户名已存在");
                return ResponseEntity.ok(response);
            }
        } catch (Exception e) {
            logger.error("Registration error for user: " + username, e);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "服务器错误：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
} 